#pragma once
#include <string>
#include <map>
extern "C" {
#include <event2/event.h>
#include <event.h>
#include <event2/listener.h>
#include "event2/thread.h"
};
class ServiceHandler;
struct TimerInfo;
class Service
{
public:
	Service();
	bool start();
	void stop();
	event_base* getBase();
	void sendCommand(std::string cmd, void* data);
	void setHandler(ServiceHandler* handler);
	void addTimer(int timerId, int timeoutMs);
	void stopTimer(int timerId);
	bool timerPending(int timerId);
private:
	bool procCommand(int type, void* data);
private:
	static void onTimer(evutil_socket_t fd, short event, void* data);
	static void onChannelRecv(bufferevent* bev, void* data);
	static DWORD WINAPI doWork(void* data);
private:
	event_config* m_config;
	event_base* m_base;
	evutil_socket_t m_sockets[2];
	bufferevent* m_bevRecv;
	CRITICAL_SECTION m_cri;
	bool m_running;
	HANDLE m_thread;
	ServiceHandler* m_handler;
	std::map<int, TimerInfo*> m_mapTimer;
};
